package com.mzy;

import java.util.Arrays;

public class RandToRand {

    public static void main(String[] args) {
        int testTimes = 10000000;
        int count = 0;
        for (int i = 0; i < testTimes; i++) {
            if (Math.random() < 0.75) {
                count++;
            }
        }
        System.out.println((double) count / testTimes);


        System.out.println("===========================");
        count = 0;
        double x = 0.3;
        for (int i = 0; i < testTimes; i++) {
            if (xToXPower2() < x) {
                count++;
            }
        }
        System.out.println((double) count / testTimes);
        System.out.println(Math.pow(x, 2));

        System.out.println("===========================");
        int[] arr = new int[2];
        for (int i = 0; i < testTimes; i++) {
            arr[numToZeroAndOne()]++;
        }
        System.out.println(Arrays.toString(arr));


        System.out.println("=====================");

        arr = new int[7];
        for (int i = 0; i < testTimes; i++) {
            arr[f2()]++;
        }
        System.out.println(Arrays.toString(arr));
    }

    public static double xToXPower2() {
        return Math.max(Math.random(), Math.random());
    }

    /**
     * f1函数
     *
     * @return 等概率返回 1-5 内的任意
     */
    public static int f1() {
        return (int) (Math.random() * 5) + 1;
    }

    /**
     * @return 等概率返回 0 和 1
     */
    public static int numToZeroAndOne() {
        int ans = 0;
        do {
            ans = f1();
        } while (ans == 3);
        return ans > 3 ? 0 : 1;
    }

    /**
     * 000~111
     *
     * @return 等概率返回 0-7
     */
    public static int returnZeroToSeven() {
        int ans = (numToZeroAndOne() << 2) + (numToZeroAndOne() << 1) + (numToZeroAndOne() << 0);
        return ans;
    }

    public static int f2() {
        int ans = 0;
        do {
            ans = returnZeroToSeven();
        } while (ans == 7);
        return ans;
    }
}
